<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="preferenceList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="preferenceId"/>
      <el-table-column label="优选名称" align="center" prop="preferenceName"/>
      <el-table-column label="是否推荐" align="center" prop="preferenceStatus">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.preferenceStatus"
            :active-value="1"
            :inactive-value="0"
            @change="updatePreferenceStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="isSort"/>
      <el-table-column label="状态" align="center" prop="preferenceStatus">
        <template slot-scope="scope">
            <span v-if="scope.row.preferenceStatus == 0">不推荐</span>
            <span v-if="scope.row.preferenceStatus == 1">推荐中</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:post:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:post:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[3, 5, 7, 10]"
      @size-change="handleSizeChange"
      :page.sync="preferenceListRequest.pageNum"
      :limit.sync="preferenceListRequest.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优选对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="preference" :rules="rules" label-width="80px">
        <el-form-item label="优选名称" prop="preferenceName">
          <el-input v-model="preference.preferenceName" placeholder="请输入优选名称"/>
        </el-form-item>
        <el-form-item label="是否推荐" prop="preferenceStatus">
          <el-radio v-model="preference.preferenceStatus" :label="1">是</el-radio>
          <el-radio v-model="preference.preferenceStatus" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="排序" prop="isSort">
          <el-input v-model="preference.isSort" placeholder="请设置排序"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import ImageUpload from "../../../components/ImageUpload/index.vue";
import {
  addPreference,
  deletePreference,
  findPreferenceList,
  updatePreference
} from "../../../api/marketing/preference/preference";

export default {
  name: "preference",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
      //上传图片
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销优选表格数据
      preferenceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      preferenceListRequest: {
        pageNum: 1,
        pageSize: 5
      },
      // 表单参数
      preference: {},
      // 表单校验
      rules: {
        preferenceName: [
          {required: true, message: "优选名称不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  comments:{
   imageUpload:ImageUpload
  },
  methods: {
    updatePreferenceStatus(row) {
      if (row.preferenceId != undefined) {
        updatePreference(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      }
    }, handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.preferenceListRequest.pageSize = val;
      this.getList()
    },
    /** 查询营销优选列表 */
    getList() {
      this.loading = true;
      findPreferenceList(this.preferenceListRequest).then(response => {
        this.preferenceList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.preference = {
        preferenceId: 0,
        preferenceName: '',
        preferenceStatus: 1,
        isSort: 0,
        isDelete: 0
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subjectId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.preference = row;
      this.open = true;
      this.title = "修改营销优选";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加营销优选";
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.subject)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.preference.preferenceId != 0) {
            updatePreference(this.preference).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPreference(this.preference).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.preferenceId || this.ids;
      this.$modal.confirm('是否确认删除优选编号为"' + postIds + '"的数据项？').then(function () {
        return deletePreference(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
